<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="searchForm.created_at"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:ii:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="用户">
        <el-input clearable v-model="searchForm.user_id" placeholder="搜索用户"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95">
        <template v-slot:default="{scope,row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="ip" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.ip||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="app版本" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.app_version||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="定义来源" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.bs_from||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="系统" width="100">
        <template v-slot:default="{scope,row}">
          {{ row.os }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="终端" width="100">
        <template v-slot:default="{scope,row}">
          {{ row.platform }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.user_id>0"   type="primary" @click="$router.push({ path: '/user/index',query:{user_id:row.user_id} })">{{row.user_account||row.user_id}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="left" label="地区" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.address||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="路径" width="200">
        <template v-slot:default="{scope,row}">
         {{ row.method ||'unknown' }} : {{ row.url||'' }}
        </template>
      </el-table-column>
      <el-table-column label="请求时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>


      <el-table-column align="center" prop="created_at" label="操作" width="200">
        <template v-slot:default="{scope,row}">
          <el-row>
            <el-button size="mini" type="primary" icon="el-icon-view" circle
                       @click="$router.push({path:'access_log_form',query:{id:row.id}})"
            ></el-button>
            <el-button size="mini" type="info" icon="el-icon-delete" circle @click="del(row.id)"></el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/access_log'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'gray',
        '-1': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {

        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ id: id }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }

}
</script>
